<template>
  <div class="my-filter-content">
    <div class="my-fc-search">
      <div class="my-fc-search-top">
        <vxe-input v-model="option.data.sVal"
                   placeholder="搜索"
                   @change="searchEvent"></vxe-input>
      </div>
      <div class="my-fc-search-content">
        <template v-if="valList.length">
          <ul class="my-fc-search-list my-fc-search-list-head">
            <li class="my-fc-search-item">
              <vxe-checkbox v-model="isAll"
                            @change="changeAllEvent">全选</vxe-checkbox>
            </li>
          </ul>
          <ul class="my-fc-search-list my-fc-search-list-body">
            <li class="my-fc-search-item"
                v-for="(item, sIndex) in valList"
                :key="sIndex">
              <vxe-checkbox v-model="item.checked">{{ item.value }}</vxe-checkbox>
            </li>
          </ul>
        </template>
        <template v-else>
          <div class="my-fc-search-empty">无匹配项</div>
        </template>
      </div>
    </div>
    <div class="my-fc-footer">
      <vxe-button status="primary"
                  @click="confirmFilterEvent">确认</vxe-button>
      <vxe-button @click="resetFilterEvent">重置</vxe-button>
    </div>
  </div>
</template>

<script>
import XEUtils from 'xe-utils'

export default {
  name: 'FilterContent',
  props: {
    params: Object,
    // options: Array,
  },
  data() {
    return {
      isAll: false,
      option: null,
      colValList: [],
      valList: [],
      count: '',
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      //   console.log(this.options)
      const { $table, column } = this.params
      const { fullData } = $table.getTableData()
      const option = column.filters[0]
      const { vals } = option.data
      const colValList = Object.keys(
        XEUtils.groupBy(fullData, column.property)
      ).map((val) => {
        return {
          checked: vals.includes(val),
          value: val,
        }
      })
      this.option = option
      this.colValList = colValList
      this.valList = colValList
    },
    //搜索事件
    searchEvent() {
      const { option, colValList } = this
      const sVal = XEUtils.toValueString(option.data.sVal).toLowerCase()
      this.valList = option.data.sVal
        ? colValList.filter(
            (item) =>
              XEUtils.toValueString(item.value).toLowerCase().indexOf(sVal) > -1
          )
        : colValList
    },
    //全选事件
    changeAllEvent() {
      console.log('changeAllEvent')
      const { isAll } = this
      this.valList.forEach((item) => {
        item.checked = isAll
      })
    },
    //确认筛选
    confirmFilterEvent(evnt) {
      console.log('confirmFilterEvent')
      const { params, option, valList } = this
      const { data } = option
      const { $panel } = params
      data.vals = valList
        .filter((item) => item.checked)
        .map((item) => item.value)
      $panel.changeOption(evnt, true, option)
      $panel.confirmFilter()
    },
    //重置筛选
    resetFilterEvent() {
      console.log('resetFilterEvent')
      const { $panel } = this.params
      $panel.resetFilter()
    },
  },
}
</script>

<style>
.my-filter-content {
  padding: 10px;
  user-select: none;
}
.my-filter-content .my-fc-search .my-fc-search-top {
  position: relative;
  padding: 5px 0;
}
.my-filter-content .my-fc-search .my-fc-search-top > input {
  border: 1px solid #ababab;
  padding: 0 20px 0 2px;
  width: 200px;
  height: 22px;
  line-height: 22px;
}
.my-filter-content .my-fc-search .my-fc-search-content {
  padding: 2px 10px;
}
.my-filter-content .my-fc-search-empty {
  text-align: center;
  padding: 20px 0;
}
.my-filter-content .my-fc-search-list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.my-filter-content .my-fc-search-list-body {
  overflow: auto;
  height: 120px;
}
.my-filter-content .my-fc-search-list .my-fc-search-item {
  padding: 2px 0;
  display: block;
}
.my-filter-content .my-fc-footer {
  text-align: right;
  padding-top: 10px;
}
.my-filter-content .my-fc-footer button {
  padding: 0 15px;
  margin-left: 15px;
}
</style>